package com.user.basic.analyze.register;

import com.entity.analyze.RegisterType;
import org.springframework.stereotype.Service;

import java.lang.ref.Reference;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class YearRegisterAnalyzeServiceImpl extends RegisterAnalyzeServiceImpl {
    public YearRegisterAnalyzeServiceImpl(RegisterAnalyzeMapper registerAnalyzeMapper) {
        super(registerAnalyzeMapper);
    }

    @Override
    protected Map<Integer, Integer> transform(Reference<ConcurrentHashMap<String, List<Date>>> cache) {
        if (Objects.requireNonNull(cache.get()).size() >0){
            Map<Integer, Integer> countMap = new HashMap<>();
            cache.get().forEach((k, v) -> v.forEach(date -> countMap.compute((date.getYear() + 1900),
                    RegisterAnalyzeServiceImpl::apply)
            ));
            return countMap;
        }
      return null;
    }

    @Override
    protected void save(Map<Object, Integer> transform) {
        save(transform, RegisterType.Year);
    }
}
